<template>
  <!-- 上传俩种方式 action http-request  action等于发送了一次post请求-->

  <!-- :http-request="uploade" -->
  <!-- 给组件注册方法需要用native -->
  <!-- show-file-list 是否显示已上传文件列表 -->
  <!-- on-success 文件上传成功时的钩子 -->

  <!-- $attrs可以获取组件除prop外的所有属性-->
  <!-- :action="$attrs.url || ''"   没传url则为空 -->
  <div>
    <el-upload
      class="avatar-uploader"
      :action="$attrs.url || ''"
      :on-success="onSuccess"
      :[httpRequest]="uploade"
      :show-file-list="false"
      :before-upload="beforeUpload"
      @mouseenter.native="maskShow=true"
      @mouseleave.native="maskShow=false"
    >
      <img v-if="value" class="avatar" :src="value">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <!-- 阻止冒泡 -->
      <div v-if="maskShow&&value" class="mask" @click.stop>
        <i class="el-icon-zoom-in" @click.stop="handPreview" />
        <i class="el-icon-delete" @click.stop="handleDel" />
      </div>
    </el-upload>
    <!-- 进度条 -->
    <el-progress v-if="showPercent" :percentage="percenTage" style="width:200px;margin-top:10px;" />
    <!-- 预览 -->
    <el-dialog :visible.sync="showDialog" title="图片预览">
      <img :src="value" alt="" style="width:100%">
    </el-dialog>
  </div>
</template>
<script>
// 0.导入
import COS from 'cos-js-sdk-v5'

// SECRETID 和 SECRETKEY 请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
// 1.登录腾讯云cos
var cos = new COS({
  SecretId: 'AKIDzlS5XotoYKsGXjoeKxaAf4KOG3hGftvl',
  SecretKey: 'iDofXOip03sSyJSXS9bnkRvCG7lALFtO'
})
export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      maskShow: false,
      showDialog: false,
      percenTage: 0,
      showPercent: false
    }
  },
  computed: {
    httpRequest() {
      return this.$attrs.url ? '' : 'http-request'
    }
  },
  methods: {
    uploade(params) {
      cos.putObject({
        Bucket: 'guo1-1317386383', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-nanjing', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: params.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: params.file, /* 必须，上传文件对象，可以是input[type="file"]标签选择本地文件后得到的file对象 */
        onProgress: (progressData) => {
          this.showPercent = true
          this.percenTage = progressData.percent * 100
        }
      }, (err, data) => { // 需要是箭头函数 this指向
        // 3.上传成功更改图片地址
        // console.log(err || data)
        if (err) {
          this.$message.error('上传失败')
        } else {
          const url = 'https://' + data.Location
          this.$emit('input', url)
          setTimeout(() => {
            this.showPercent = false
          }, 300)
        }
      })
    },
    handleDel() {
      this.$emit('input', '')
    },
    handPreview() {
      this.showDialog = true
    },
    beforeUpload(file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      const maxSize = 100 * 1024
      if (!types.includes(file.type)) {
        this.$message.error('不支持此格式')
        return false
      }
      if (file.size > maxSize) {
        this.$message.error('文件过大')
        return false
      }
    //   return true
    },

    onSuccess(response) {
      this.$emit('input', response.url)
    }
  }
}
</script>

<style>
  .avatar-uploader{
    width: 178px;
    height: 178px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .mask{
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: rgba(0,0,0,0.2);
  }
  i{
     font-size: 25px;
   }

  </style>
